home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #27 (Dec 87) / Pascal MIDI shell / MIDIShell.Pas < prev    next >
Pascal/Delphi Source File  |  1987-07-15  |  6KB  |  289 lines

  1. { Kirk Austin, 7/12/87 }
  2. { This is an example program that illustrates the following techniques: }
  3. { My preferred method for handling the about box }
  4. {The use of the transfer command in the file menu }
  5. {The use of the LSPMIDI library including MIDIThru}
  6.  
  7. PROGRAM ShellExample;
  8.  
  9.     USES
  10.         LSPMIDI;
  11.  
  12. { Global Constants }
  13.     CONST
  14.         Null = '';
  15.  
  16.         AppleMenuID = 1;
  17.  
  18.         FileMenuID = 2;
  19.  
  20.         EditMenuID = 3;
  21.  
  22.         MIDIMenuID = 4;
  23.  
  24.         AboutID = 200;
  25.  
  26. { Global Variables }
  27.  
  28.     VAR
  29.         myMenus : ARRAY[AppleMenuID..MIDIMenuID] OF MenuHandle;
  30.         Done : Boolean;            { true when user selects quit}
  31.  
  32. {This is a way to do the about box so that it doesn't interfere with the application.}
  33. {For instance, you can make menu selections while the about box is visible.}
  34.     PROCEDURE ShowAbout;
  35.         VAR
  36.             theDlog : DialogPtr;
  37.             oldPort : GrafPtr;
  38.     BEGIN
  39.         GetPort(oldPort);
  40.         theDlog := GetNewDialog(AboutID, NIL, Pointer(-1));
  41.         SetPort(theDlog);
  42.         DrawDialog(theDlog);
  43.         WHILE NOT Button DO
  44.             ;
  45.         DisposDialog(theDlog);
  46.         SetPort(oldPort);
  47.     END;
  48.  
  49.     PROCEDURE LaunchIt (mode : integer;
  50.                                     VAR fName : Str255);
  51.     INLINE
  52.         $204F,     {movea.l  a7,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  53.         $A9F2;    {_Launch}
  54.  
  55.  
  56.     PROCEDURE DoXfer;
  57.         VAR
  58.             where : Point;
  59.             reply : SFReply;
  60.             vRef : integer;
  61.             thefName : Str255;
  62.             textType : SFTypeList;
  63.     BEGIN
  64.         where.h := 80;
  65.         where.v := 55;
  66.         textType[0] := 'APPL';
  67.         SFGetFile(where, Null, NIL, 1, textType, NIL, reply);
  68.         WITH reply DO
  69.             IF NOT good THEN
  70.                 thefName := Null
  71.             ELSE
  72.                 BEGIN
  73.                     thefName := fName;
  74.                     vRef := vRefNum
  75.                 END;
  76.         IF thefName <> Null THEN
  77.             BEGIN
  78.                 Done := true;
  79.                 IF SetVol(NIL, vRef) = noErr THEN
  80.                     BEGIN
  81.                         ResetSCCA;
  82.                         ResetSCCB;
  83.                         QuitTimer;
  84.                         LaunchIt(0, thefName)
  85.                     END;
  86.             END
  87.     END;
  88.  
  89.     PROCEDURE ProcessMenu (codeWord : Longint);    { handle menu selections}
  90.         VAR
  91.             i : integer;
  92.             menuNum : Integer;
  93.             TheMenuHdle : MenuHandle;
  94.             itemNum : Integer;
  95.             NameHolder : str255;
  96.             dummy : Integer;
  97.             ignore : boolean;
  98.             TheValue : longint;
  99.  
  100.     BEGIN
  101.         IF codeWord <> 0 THEN    { nothing was selected}
  102.             BEGIN
  103.                 menuNum := HiWord(codeWord);
  104.                 itemNum := LoWord(codeWord);
  105.                 CASE menuNum OF { the different menus}
  106.                     AppleMenuID : 
  107.                         BEGIN
  108.                             IF itemNum < 3 THEN
  109.                                 BEGIN
  110.                                     ShowAbout;
  111.                                 END
  112.                             ELSE
  113.                                 BEGIN
  114.                                     GetItem(myMenus[AppleMenuID], itemNum, NameHolder);
  115.                                     dummy := OpenDeskAcc(NameHolder);
  116.                                 END;
  117.                         END;
  118.                     FileMenuID : 
  119.                         BEGIN
  120.                             CASE ItemNum OF
  121.                                 1 : 
  122.                                     BEGIN
  123.                                         DoXfer;
  124.                                     END;
  125.                                 2 : 
  126.                                     BEGIN
  127.                                         Done := true;
  128.                                     END;
  129.                             END;
  130.                         END;
  131.                     EditMenuID : 
  132.                         BEGIN
  133.                             ignore := SystemEdit(itemNum - 1);
  134.                         END;
  135.                     MIDIMenuID : 
  136.                         BEGIN
  137.                             TheMenuHdle := GetMHandle(4);
  138.                             FOR i := 1 TO 5 DO
  139.                                 CheckItem(TheMenuHdle, i, false);
  140.                             MIDIThruA(0);
  141.                             MIDIThruB(0);
  142.                             CASE ItemNum OF
  143.                                 1 : 
  144.                                     BEGIN
  145.                                         CheckItem(TheMenuHdle, 1, true);
  146.                                         MIDIThruA(1);
  147.                                     END;
  148.                                 2 : 
  149.                                     BEGIN
  150.                                         CheckItem(TheMenuHdle, 2, true);
  151.                                         MIDIThruA(2);
  152.                                     END;
  153.                                 3 : 
  154.                                     BEGIN
  155.                                         CheckItem(TheMenuHdle, 3, true);
  156.                                         MIDIThruB(1);
  157.                                     END;
  158.                                 4 : 
  159.                                     BEGIN
  160.                                         CheckItem(TheMenuHdle, 4, true);
  161.                                         MIDIThruB(2);
  162.                                     END;
  163.                                 5 : 
  164.                                     BEGIN
  165.                                         CheckItem(TheMenuHdle, 5, true);
  166.                                         MIDIThruA(0);
  167.                                         MIDIThruB(0);
  168.                                     END;
  169.                             END;
  170.                         END;
  171.                 END;
  172.                 HiliteMenu(0);
  173.             END;
  174.     END;
  175.  
  176. { }
  177.     PROCEDURE DealWithMouseDowns (theEvent : EventRecord);
  178.         VAR
  179.             location : Integer;
  180.             windowPointedTo : WindowPtr;
  181.             mouseLoc : point;
  182.             windowLoc : integer;
  183.             VandH : Longint;
  184.             Height : Integer;
  185.             Width : Integer;
  186.     BEGIN
  187.         mouseLoc := theEvent.where;
  188.         windowLoc := FindWindow(mouseLoc, windowPointedTo);
  189.         CASE windowLoc OF
  190.             inMenuBar : 
  191.                 BEGIN
  192.                     ProcessMenu(MenuSelect(mouseLoc));
  193.                 END;
  194.             inSysWindow : 
  195.                 BEGIN
  196.                     SystemClick(theEvent, windowPointedTo);
  197.                 END;
  198.             OTHERWISE
  199.                 BEGIN
  200.                 END;
  201.         END;
  202.     END;
  203.  
  204.     PROCEDURE DealWithKeyDowns (theEvent : EventRecord);
  205.         TYPE
  206.             Trick = PACKED RECORD
  207.                     CASE boolean OF
  208.                         true : (
  209.                                 long : Longint
  210.                         );
  211.                         false : (
  212.                                 chr3, chr2, chr1, chr0 : char
  213.                         )
  214.                 END;
  215.         VAR
  216.             CharCode : char;
  217.             TrickVar : Trick;
  218.     BEGIN
  219.         TrickVar.long := theEvent.message;
  220.         CharCode := TrickVar.chr0;
  221.         IF BitAnd(theEvent.modifiers, CmdKey) = CmdKey THEN {check for a menu selection}
  222.             BEGIN
  223.                 ProcessMenu(MenuKey(CharCode));
  224.             END
  225.     END;
  226.  
  227.     PROCEDURE MainEventLoop;
  228.         VAR
  229.             Event : EventRecord;
  230.             ProcessIt : boolean;
  231.             x : byte;
  232.             TheValue : Longint;
  233.     BEGIN
  234.         REPEAT
  235.             SystemTask;
  236.             ProcessIt := GetNextEvent(everyEvent, Event); { get the next event in queue}
  237.             IF ProcessIt THEN
  238.                 BEGIN
  239.                     CASE Event.what OF
  240.                         mouseDown : 
  241.                             DealWithMouseDowns(Event);
  242.                         AutoKey : 
  243.                             DealWithKeyDowns(Event);
  244.                         KeyDown : 
  245.                             DealWithKeyDowns(Event);
  246.                         OTHERWISE
  247.                             BEGIN
  248.                             END;
  249.                     END;
  250.                 END;
  251.         UNTIL Done;
  252.     END;
  253.  
  254.     PROCEDURE MakeMenus;        { get the menus & display them}
  255.         VAR
  256.             index : Integer;
  257.             TheMenuHdle : MenuHandle;
  258.     BEGIN
  259.         FOR index := AppleMenuID TO MIDIMenuID DO
  260.             BEGIN
  261.                 myMenus[index] := GetMenu(index);
  262.                 InsertMenu(myMenus[index], 0);
  263.             END;
  264.         AddResMenu(myMenus[AppleMenuID], 'DRVR');
  265.         DrawMenuBar;
  266.     {put a check mark on the "none" menu item by default}
  267.         TheMenuHdle := GetMHandle(4);
  268.         CheckItem(TheMenuHdle, 5, true);
  269.     END;
  270.  
  271. { Program Starts Here }
  272. BEGIN
  273.     Done := false;
  274.     FlushEvents(everyEvent, 0);
  275.  
  276.     InitSCCA;
  277.     InitSCCB;
  278.     InitTimer(782 * 5);    {increment the counter every 5 milliseconds}
  279.     StartCounter;
  280.  
  281.     MakeMenus;
  282.     InitCursor;
  283.     MainEventLoop;
  284.  
  285.     ResetSCCA;
  286.     ResetSCCB;
  287.     QuitTimer;
  288.  
  289. END.